package cn.yutong.framework.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

import static java.time.ZoneId.systemDefault;

/**
 * @className: TimeTools
 * @Description: TODO
 * @author: lnexin@aliyun.com
 * @Date: 2018/9/17 16:12
 */
public class TimeTools {
    private static final ZoneId zone = ZoneId.systemDefault();

    /**
     * 转化特定格式的日期字符串位date
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date strDateParse(String date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            System.err.println("时间转换出错!:" + date);
        }
        return parse;
    }

    public static long strDateParse2Long(String date, String pattern) {
        return strDateParse(date, pattern).getTime();
    }

    /**
     * 转换为某一天的最大或者最小时间
     *
     * @param date
     * @param pattern
     * @param isMin
     * @param isMax
     * @return
     */
    public static long convertToMinOrMax(String date, String pattern, boolean isMin, boolean isMax) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(date);
            if (isMin)
                parse = startOfDay(parse);
            if (isMax)
                parse = endOfDay(parse);
        } catch (ParseException e) {
            System.err.println("时间转换出错!:" + date);
        }
        return parse.getTime();
    }

    /**
     * 转换为某一天的最大或者最小时间
     *
     * @param longtime
     * @param isMin
     * @param isMax
     * @return
     */
    public static long convertToMinOrMax(long longtime, boolean isMin, boolean isMax) {
        if (isMin)
            return startOfDay(longtime);
        if (isMax)
            return endOfDay(longtime);
        return System.currentTimeMillis() / 1000;
    }

    // 获得某天最小时间 2017-10-15 00:00:00
    public static Date startOfDay(Date date) {
        return new Date(startOfDay(date.getTime()));
    }

    public static long startOfDay(long timestamp) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), systemDefault());
        return Date.from(localDateTime.with(LocalTime.MIN).atZone(systemDefault()).toInstant()).getTime();
    }

    // 获得某天最大时间 2017-10-15 23:59:59
    public static Date endOfDay(Date date) {
        return new Date(endOfDay(date.getTime()));
    }

    public static long endOfDay(long timestamp) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), systemDefault());
        return Date.from(localDateTime.with(LocalTime.MAX).atZone(systemDefault()).toInstant()).getTime();
    }


    /**
     * 13位时间戳转为位 2018-01-01 格式时间戳
     *
     * @param time
     * @return
     */
    public static String long2YMD(long time) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), zone).toLocalDate().toString();
    }

    /**
     * 13位 时间戳 转换为特定格式的 字符格式时间戳
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String long2Str(long time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Timestamp timestamp = new Timestamp(time);
        return format.format(timestamp);
    }

    private static LocalDate long2LocalDate(long time) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), zone).toLocalDate();
    }

    /**
     * 计算两个固定格式天数之间相差的天数
     *
     * @param start
     * @param end
     * @param pattern
     * @return
     */
    public static long differDays(String start, String end, String pattern) {
        return differDays(strDateParse2Long(start, pattern), strDateParse2Long(end, pattern));
    }

    /**
     * 计算两个Date之间相差的天数
     *
     * @param start
     * @param end
     * @return
     */
    public static long differDays(Date start, Date end) {
        return differDays(start.getTime(), end.getTime());
    }

    /**
     * 计算两个时间戳之间相差的天数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long differDays(long startTime, long endTime) {
        LocalDate startDate = long2LocalDate(startTime);
        LocalDate endDate = long2LocalDate(endTime);
        return endDate.toEpochDay() - startDate.toEpochDay() + 1;
    }
}
